package com.example.ivos.user.service.impl;

import com.example.ivos.base.exception.ServiceException;
import com.example.ivos.base.response.StatusCode;
import com.example.ivos.user.mapper.UserMapper;
import com.example.ivos.user.pojo.dto.UserLoginParam;
import com.example.ivos.user.pojo.dto.UserQuery;
import com.example.ivos.user.pojo.dto.UserSaveParam;
import com.example.ivos.user.pojo.entity.User;
import com.example.ivos.user.pojo.vo.UserVO;
import com.example.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**业务层实现类需要添加@Service注解,表示其为业务层实现类*/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login( UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVO userVO=userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题,不能直接返回结果给前端,而是要抛出业务层异常,让全局异常处理器来处理
         * 一旦抛出异常,结果是由全局异常处理器来返回给前端的,就不走controller了 */
        if(userVO == null){//用户名错误
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){//密码错误
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //只有没有遇到上方的两个异常,才会执行下面的代码,将查出来的userVO返回给controller!
        log.debug("用户查询结果：{}", userVO);
        return userVO;
    }
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        //注意:员工不是自己注册的，所以需要设置初始密码
        if (user.getId() == null){
        user.setPassword("123456");
        user.setCreateTime(new Date());
        userMapper.insert(user);}
        else{
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUsers(UserQuery userQuery) {
        log.debug("查询用户业务参数:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void updatePassword(Long userId) {
        log.debug("修改密码业务参数:{}",userId);
        User  user = new User();
        user.setId(userId);
        user.setPassword("I hate you");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("修改状态业务参数:{}",userId,status);
        User  user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void delete(Long userId) {
        if (userId == null) {
            log.warn("尝试删除用户时收到空的 userId");
            throw new IllegalArgumentException("userId 不能为空");
        }

        try {
            log.debug("开始删除用户，参数: {}", userId);
            userMapper.delete(userId);
            log.debug("成功删除用户，userId: {}", userId);
        } catch (Exception e) {
            log.error("删除用户失败，userId: {}", userId, e);
            throw e; // 或者根据业务需求封装后抛出
        }
    }

    @Override
    public ArrayList<UserVO> selectAudit(Long parentId) {
        log.debug("查询待审核用户业务参数:{}",parentId);
        UserVO userVO= userMapper.selectAudit(parentId);
        ArrayList<UserVO> list = new ArrayList<>();
        if (userVO != null&&userVO.getParentId()!=null){
            list.add(userVO);
            Long id= userVO.getParentId();
            UserVO userVO1= userMapper.selectAudit(id);
            if (userVO1 != null){
                list.add(userVO1);
            }

        }
        return list;

    }
}
